<?php
namespace app\admin\model;
use think\Model;
use think\Db;
class Exam extends Model
{
    function examData($p=1,$size=15,$where='is_del=0'){
//        dump($where);exit;
        $list = array();
        $count = Db::name('exam')->alias('e')->where($where)->count();// 查询满足要求的总记录数
        $list = Db::name('exam')
            ->alias('e')
            ->join('subject s','e.subjectid = s.subjectid','left')
            ->field('e.*,s.subject_name')
            ->where($where)
            ->order('exam_order,examid desc')
            ->paginate($size,$count,['query'=>request()->param()])
            ->each(function($item,$key){
                return $item;
            });
        $page = $list->render();

        $data['count'] = $count;
        $data['list'] = $list;
        $data['page'] = $page;
        return $data;
    }

    function getExam($id){
        if($id > 0){
            $exam = Db::name('exam')->json(['exam_setting'])->find($id);
            return $exam;
        }else{
            exitjson(0,'参数错误');
        }

    }

    function examAllList(){
        $where[]=['is_del','=',0];
        if(input('subjectid') <> ''){
            $where[] = ['subjectid','=',input('subjectid')];
        }
        $res = Db::name('exam')->where($where)->order("examid desc")->limit(50)->select();

        return $res;
    }

    //处理参数
    function disposeData($data,$act=''){
        $newData['exam_name'] = $data['exam_name'];
        $newData['subjectid'] = $data['subjectid'];
        $newData['exam_time'] = $data['exam_time'];
        $newData['exam_score'] = $data['exam_score'];
        $newData['pass_line'] = $data['pass_line'];
        $newData['exam_setting'] = json_encode($data['question_info']);
        if($act == 'add'){
            $newData['admin_id'] = session('admin_id');
            $newData['add_name'] = session('user_name');
            $newData['add_time'] = time();
        }

        return $newData;
    }

    //组装成小程序所需要的json格式
    function putSetting($exam_setting){
        $i = 1;
        //单选
        $radio = $exam_setting['radio'];
        $radioQuestions = [];
        if($radio['questionids']<>''){
            $radio_questions = Db::name('question')->json(['question_select'])->where('questionid','in',($radio['questionids']))->order("questionid")->select();
            foreach($radio_questions as $k=>$v){
                $radioQuestions[$v['questionid']]['id'] = $v['questionid'];
                $radioQuestions[$v['questionid']]['type'] = 1;
                $radioQuestions[$v['questionid']]['typeTitle'] = "一、单项选择题（本大题共".$radio['total_num']."小题，每小题".$radio['score']."分。在每小题给出的下列选项中，只有一项是符合题目要求的。）";
                $radioQuestions[$v['questionid']]['typeName'] = "选择题";
                $radioQuestions[$v['questionid']]['selectTitle'] = $i.'.'.trim(movehtml($v['question']));
                $radioQuestions[$v['questionid']]['images'] = '';
                $radioQuestions[$v['questionid']]['num'] = $i;
                $radioQuestions[$v['questionid']]['daan'] = '';
                $radioQuestions[$v['questionid']]['question_answer'] = trim(movehtml($v['question_answer']));
                $radioQuestions[$v['questionid']]['question_select'] = $v['question_select'];
                $radioQuestions[$v['questionid']]['question_analyse'] = $v['question_analyse'];
                $radioQuestions[$v['questionid']]['content'] = [];
                $radioQuestions[$v['questionid']]['question'] = $v['question'];
                $radioQuestions[$v['questionid']]['is_right'] = $v['is_right'];
                $radioQuestions[$v['questionid']]['daanIndex'] = $v['daanIndex'];
                $radioQuestions[$v['questionid']]['select'] = $v['select'];
                if($k == 0){
                    $radioQuestions[$v['questionid']]['catTitle'] = $radioQuestions[$v['questionid']]['typeTitle'];
                }
                $i++;
            }
        }
        //判断
        $judge = $exam_setting['judge'];
        $judgeQuestions = [];
        if($judge['questionids'] <> ''){
            $judge_questions = Db::name('question')->where('questionid','in',($judge['questionids']))->order("questionid")->select();
            foreach($judge_questions as $k=>$v){
                $judgeQuestions[$v['questionid']]['id'] = $v['questionid'];
                $judgeQuestions[$v['questionid']]['type'] = 2;
                $judgeQuestions[$v['questionid']]['typeTitle'] = "二、判断题（本大题共".$judge['total_num']."小题，每小题".$judge['score']."分。在每小题给出的下列选项中，只有一项是符合题目要求的。）";
                $judgeQuestions[$v['questionid']]['typeName'] = "判断题";
                $judgeQuestions[$v['questionid']]['selectTitle'] = $i.'.'.trim(movehtml($v['question']));
                $judgeQuestions[$v['questionid']]['images'] = '';
                $judgeQuestions[$v['questionid']]['num'] = $i;
                $judgeQuestions[$v['questionid']]['daan'] = $v['question_answer'];
                $judgeQuestions[$v['questionid']]['question_answer'] = trim(movehtml($v['question_answer']));
                $judgeQuestions[$v['questionid']]['question_select'] = $v['question_select'];
                $judgeQuestions[$v['questionid']]['question_analyse'] = $v['question_analyse'];
                $judgeQuestions[$v['questionid']]['content'] = [];
                $judgeQuestions[$v['questionid']]['question'] = $v['question'];
                $judgeQuestions[$v['questionid']]['is_right'] = $v['is_right'];
                $judgeQuestions[$v['questionid']]['daanIndex'] = $v['daanIndex'];
                $judgeQuestions[$v['questionid']]['select'] = $v['select'];
                if($k==0){
                    $judgeQuestions[$v['questionid']]['catTitle'] = $judgeQuestions[$v['questionid']]['typeTitle'];
                }
                $i++;
            }
        }
//        多选
        $check = $exam_setting['check'];
        $checkQuestions = [];
        if($check['questionids']<>''){
            $check_questions = Db::name('question')->json(['question_select'])->where('questionid','in',($check['questionids']))->order("questionid")->select();
            foreach($check_questions as $k=>$v){
                $checkQuestions[$v['questionid']]['id'] = $v['questionid'];
                $checkQuestions[$v['questionid']]['type'] = 3;
                $checkQuestions[$v['questionid']]['typeTitle'] = "三、多项选择题（本大题共".$check['total_num']."小题，每小题".$check['score']."分。在每小题给出的下列选项中，至少有一项是符合题目要求的。）";
                $checkQuestions[$v['questionid']]['typeName'] = "选择题";
                $checkQuestions[$v['questionid']]['selectTitle'] = $i.'.'.trim(movehtml($v['question']));
                $checkQuestions[$v['questionid']]['images'] = '';
                $checkQuestions[$v['questionid']]['num'] = $i;
                $checkQuestions[$v['questionid']]['daan'] = $v['question_answer'];
                $checkQuestions[$v['questionid']]['question_answer'] = trim(movehtml($v['question_answer']));
                $checkQuestions[$v['questionid']]['question_select'] = $v['question_select'];
                $checkQuestions[$v['questionid']]['question_analyse'] = $v['question_analyse'];
                $checkQuestions[$v['questionid']]['content'] = [];
                $checkQuestions[$v['questionid']]['question'] = $v['question'];
                $checkQuestions[$v['questionid']]['is_right'] = $v['is_right'];
                $checkQuestions[$v['questionid']]['daanIndex'] = $v['daanIndex'];
                $checkQuestions[$v['questionid']]['select'] = $v['select'];
                if($k==0){
                    $checkQuestions[$v['questionid']]['catTitle'] = $checkQuestions[$v['questionid']]['typeTitle'];
                }
                $i++;
            }
        }
        //填空
        $fill = $exam_setting['fill'];
        $fillQuestions = [];
        if($fill['questionids']<>''){
            $fill_questions = Db::name('question')->where('questionid','in',($fill['questionids']))->order("questionid")->select();
            foreach($fill_questions as $k=>$v){
                $fillQuestions[$v['questionid']]['id'] = $v['questionid'];
                $fillQuestions[$v['questionid']]['type'] = 4;
                $fillQuestions[$v['questionid']]['typeTitle'] = "四、填空题（本大题共".$fill['total_num']."小题，每小题".$fill['score']."分。在每小题给出的横线上填写正确答案。）";
                $fillQuestions[$v['questionid']]['typeName'] = "填空题";
                $fillQuestions[$v['questionid']]['selectTitle'] = $i.'.'.trim(movehtml($v['question']));
                $fillQuestions[$v['questionid']]['images'] = '';
                $fillQuestions[$v['questionid']]['num'] = $i;
                $fillQuestions[$v['questionid']]['daan'] = $v['question_answer'];
                $fillQuestions[$v['questionid']]['question_answer'] = trim(movehtml($v['question_answer']));
                $fillQuestions[$v['questionid']]['question_select'] = $v['question_select'];
                $fillQuestions[$v['questionid']]['question_analyse'] = $v['question_analyse'];
                $fillQuestions[$v['questionid']]['content'] = [];
                $fillQuestions[$v['questionid']]['question'] = $v['question'];
                $fillQuestions[$v['questionid']]['is_right'] = $v['is_right'];
                $fillQuestions[$v['questionid']]['daanIndex'] = $v['daanIndex'];
                $fillQuestions[$v['questionid']]['select'] = $v['select'];
                if($k==0){
                    $fillQuestions[$v['questionid']]['catTitle'] = $fillQuestions[$v['questionid']]['typeTitle'];
                }
                $i++;
            }
        }

//        $newArr = array_merge($radioQuestions,$judgeQuestions,$checkQuestions,$fillQuestions);
        $newArr = $radioQuestions + $judgeQuestions + $checkQuestions + $fillQuestions;
        return $newArr;
    }


}